<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="hbName">
        <el-input
          v-model="queryParams.hbName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="contractId">
        <el-input
          v-model="queryParams.contractId"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" prop="createDate">
        <el-date-picker
          v-model="queryParams.createDate"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sales:contract:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sales:contract:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sales:contract:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="printt"
          v-show="hsShow"
        >执行</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="printt"
          v-show="bhsShow"
        >执行打印</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          v-loading.fullscreen.lock="fullscreenLoading"
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleOrder"
        >生成订单</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="info"-->
<!--          plain-->
<!--          icon="el-icon-upload2"-->
<!--          size="mini"-->
<!--          @click="handleImport"-->
<!--        >导入</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['sales:contract:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="calc(100vh - 300px)" v-loading="loading" :data="contractList" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司代码" align="center" prop="gcCode" />
      <el-table-column label="公司名称" align="center" prop="gcName"/> >
      <el-table-column label="合同编号" align="center" prop="contractId" width="130">
        <template slot-scope="scope">
          <div style="cursor:pointer;text-decoration:underline;color: blue" class="teamUserNum" @click="htInfo(scope.row)">{{ scope.row.contractId }}</div>
        </template>
      </el-table-column>
      <el-table-column label="客户代码" align="center" prop="hbCode" />
      <el-table-column label="客户名称" align="center" prop="hbName" width="230" show-overflow-tooltip/>
      <el-table-column label="合同签订日期" align="center" prop="contractQdDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractQdDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同交货日期" align="center" prop="contractJhDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractJhDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同签订人" align="center" prop="contractQdName" width="120"/>
      <el-table-column label="是否生成订单" align="center" prop="isGenOrder" width="140" >
        <template slot-scope="scope">
          <span v-if="scope.row.isGenOrder=='0'">未生成</span>
          <span v-if="scope.row.isGenOrder=='1'" style="color: #13ce66">已生成</span>
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="attach" width="120">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            v-if="scope.row.attach!=null"
            @click="showAnnex(scope.row)"
          >附件查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="创建人" align="center" prop="createBy" width="120"/>
      <el-table-column label="合同说明" width="220" show-overflow-tooltip  align="center"  prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-if="scope.row.isGenOrder=='0'"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sales:contract:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            v-if="scope.row.isGenOrder=='0'"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sales:contract:remove']"
          >删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同管理表对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="伙伴名称" prop="hbName">
              <el-select @change="nameSelect" v-model="form.clientCode" placeholder="请选择伙伴">
                <el-option
                  v-for="item in hbList"
                  :key="item.clientCode"
                  :label="item.clientName"
                  :value="item.clientCode">
                  <span style="float: left">{{ item.clientName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.clientCode }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="伙伴代码" prop="hbCode">
              <el-input v-model="form.hbCode" placeholder="" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="伙伴类型" prop="hbType">
              <el-select v-model="form.hbType" placeholder="请选择客户类型" clearable disabled>
                <el-option
                  v-for="dict in dict.type.cus_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="伙伴税号" prop="hbShuihao">
              <el-input v-model="form.hbShuihao" placeholder="" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="伙伴地址" prop="hbAddress">
              <el-input v-model="form.hbAddress" placeholder="" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="伙伴电话" prop="hbPhone">
              <el-input v-model="form.hbPhone" placeholder="" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="伙伴开户银行" prop="hbBank">
              <el-input v-model="form.hbBank" placeholder="" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="伙伴账号" prop="hbCard">
              <el-input v-model="form.hbCard" placeholder="" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="伙伴联系人" prop="hbName2">
              <el-input v-model="form.hbName2" placeholder="" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同签订日期" prop="contractQdDate">
              <el-date-picker clearable
                              v-model="form.contractQdDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择合同签订日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同交货日期" prop="contractJhDate">
              <el-date-picker clearable
                              v-model="form.contractJhDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择合同交货日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同签订人" prop="contractQdName">
              <el-input v-model="form.contractQdName" placeholder="请输入合同签订人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同价格(元)" prop="contractUnitPrice">
              <el-input v-model="form.contractUnitPrice" placeholder="请输入合同单价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户合同号" prop="clientContractNo">
              <el-input v-model="form.clientContractNo" placeholder="请输入客户合同号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="附件">
              <image-upload v-model="form.attach"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="合同说明" prop="remark">
          <el-input autosize type="textarea" v-model="form.remark" placeholder="请输入合同说明"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 物料表对话框 -->
    <el-dialog :close-on-click-modal="false" title="合同详情" :visible.sync="wuOpen" width="1200px" append-to-body>
      <el-table  :data="itembzinfoList" >
        <el-table-column label="公司代码" fixed="left"  align="center" prop="factoryNo" />
        <el-table-column label="报价单号" fixed="left"  align="center" prop="bjdH" width="140" />
        <el-table-column label="物料名称" fixed="left"  align="center" prop="productName" />
        <el-table-column label="规格型号" fixed="left"   align="center" prop="specification" />
        <el-table-column label="客户名称"   align="center" prop="clientName" width="120"/>
        <el-table-column label="客户代码"   align="center" prop="unit" />
        <el-table-column label="外径"   align="center" prop="outDia"  width="150"/>
        <el-table-column label="内径"  align="center" prop="innerDia" />
        <el-table-column label="法兰厚度"  align="center" prop="flThick" />
        <el-table-column label="订单数量"  align="center" prop="orderNum" />
        <el-table-column label="合计成本价"  align="center" prop="hjcbj" width="120"/>
        <el-table-column label="实际报价"  align="center" prop="sjbj" />
        <el-table-column label="含税报价"  align="center" prop="hsbj" />
        <el-table-column label="报价日期"  align="center" prop="bjDate" />
        <el-table-column label="报价人"  align="center" prop="bjr" />
        <el-table-column label="报价有效期"  align="center" prop="bjyxsj" width="120"/>
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      title="审批"
      :visible.sync="dialogVisible"
      width="30%"
      >
      <el-form label="选择：" prop="remark">
        <el-form-item>
          <span style="margin-left:25%">
            <el-radio v-model="radio" label="1">审批通过</el-radio>
            <el-radio v-model="radio" label="2">审批拒绝</el-radio>
          </span>
        </el-form-item>
        <el-form-item label="批注：">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入内容"
            v-model="textarea2">
          </el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="applQd">确 定</el-button>
  </span>
    </el-dialog>



    <div style="display: none">
      <div id="printBill" ref="printBill" style="height: 700px;padding-top:10px;width: 100%;">
        <div style="width: 96%;margin: 0 auto">
          <div id="title" style="width: 100%;height: 30px;line-height: 30px;text-align: center;fontSize: 30px">购 销 合 同</div>
          <div style="margin-top:15px">甲方：XXXXXXXXXXXXXXXX</div>
          <div>乙方：{{info2.clientName}}</div>
          <div>订单号：{{info.contractId}}</div>
          <div style="float:right;color:red">{{info.contractQdDate}}</div>
          <div style="margin-top:40px">甲方给乙方供应法兰，尺寸、价格如下：</div>
          <table  border="1" style="width: 100%;">
            <tr>
              <th>品名</th>
              <th>规格</th>
              <th>数量</th>
              <th>单价</th>
              <th>合计</th>
              <th>备注</th>
            </tr>
            <tr v-for="(item,index) in cpList" :key="index">
              <td>{{item.productName}}</td>
              <td>{{item.outDia+'*'+item.innerDia+'*'+item.flThick}}</td>
              <td>{{item.orderNum}}</td>
              <td>{{item.hsbj}}</td>
              <td>{{NumberMul(item.orderNum,item.hsbj)}}</td>
              <td>{{item.remark}}</td>
            </tr>
            <tr>
              <td>合计</td>
              <td></td>
              <td>{{sum}}</td>
              <td></td>
              <td>{{totalP}}</td>
              <td></td>
            </tr>
            <!--        <tr>-->
            <!--          <td>1111</td>-->
            <!--          <td>1111</td>-->
            <!--          <td>1111</td>-->
            <!--          <td>1111</td>-->
            <!--          <td>1111</td>-->
            <!--          <td>1111</td>-->
            <!--        </tr>-->
          </table>
          <div style="display: flex;flex-direction: column;margin-top:10px">
            <div>{{info.remark}}</div>
          </div>
          <div style="width: 100%;display: flex;margin-top:40px">
            <div style="width: 52%;float: left">
              <div>甲方：博利达</div>
              <div>账户：153333688</div>
              <div>开户行：XXXXXXXXXXXXXX</div>
              <div>地址：XXXXXXXXXX</div>
              <div>电话：18615239566</div>
            </div>
            <div style="width: 48%;float: left;margin-left:15px">
              <div>乙方：{{info2.clientName}}</div>
              <div>账户：{{info2.clientAcc}}</div>
              <div>开户行：{{info2.clientBank}}</div>
              <div>地址：{{info2.clientAdress}}</div>
              <div>电话：{{info2.clientPhone}}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div style="display: none">
      <div id="printBillbhs" ref="printBillbhs" style="height: 700px;padding-top:10px;width: 100%;">
        <div style="width: 96%;margin:0 auto">
          <div id="title1" style="width: 100%;height: 30px;line-height: 30px;text-align: center;fontSize: 30px">购 销 合 同</div>
          <div style="margin-top:15px">甲方：博利达</div>
          <div>乙方：{{pYf}}</div>
          <div>订单号：{{info.contractId}}</div>
          <div style="float:right;color:red">{{info.contractQdDate}}</div>
          <div style="margin-top:40px">XXXXXXXXXXXXXX：</div>
          <table  border="1" style="width: 100%;">
            <tr>
              <th>品名</th>
              <th>规格</th>
              <th>数量</th>
              <th>单价</th>
              <th>合计</th>
              <th>备注</th>
            </tr>
            <tr v-for="(item,index) in cpList" :key="index">
              <td>{{item.productName}}</td>
<!--              <td>{{item.specification}}</td>-->
              <td>{{item.outDia+'*'+item.innerDia+'*'+item.flThick}}</td>
              <td>{{item.orderNum}}</td>
              <td>{{item.hsbj}}</td>
              <td>{{NumberMul(item.orderNum,item.hsbj)}}</td>
              <td>{{item.remark}}</td>
            </tr>
            <tr>
              <td>合计</td>
              <td></td>
              <td>{{sum}}</td>
              <td></td>
              <td>{{totalP}}</td>
              <td></td>
            </tr>
          </table>
          <div style="display: flex;flex-direction: column;margin-top:10px">
            <div>{{info.remark}}</div>
          </div>
          <div style="width: 100%;display: flex;margin-top:40px">
            <div style="width: 52%;float: left;margin-left:15px">
              <div>甲方：XXXXXXXXXXXXXXX</div>
              <div>账户：XXXXXXXX</div>
              <div>账户名：XXX</div>
              <div>地址：XXXXXXXXXXXXXXXXXX</div>
              <div>电话：1111111111</div>
            </div>
            <div style="width: 48%;float: left">
              <div>乙方：{{pYf}}</div>
              <div>账户：</div>
              <div>开户行：</div>
              <div>地址：</div>
              <div>电话：</div>
            </div>
          </div>
        </div>
      </div>
    </div>


    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import print from 'print-js';
  import {
    listContract,
    getContract,
    delContract,
    addContract,
    updateContract,
    applQd,
    scOrder
  } from "@/api/sales/contract";
  import {getToken} from "@/utils/auth";
  import {listFiles} from "@/api/sales/clientFiles";
  import moment from "moment";

export default {
  name: "Contract",
  dicts: ['customer_cate','cus_type'],
  data() {
    return {
      printBhs: {
        id: "printBillbhs", // 这里是要打印元素的ID
        popTitle: "", // 打印的标题
        extraCss: "", // 打印可引入外部的一个 css 文件
        extraHead: "", // 打印头部文字
      },
      printHs: {
        id: "printBill", // 这里是要打印元素的ID
        popTitle: "", // 打印的标题
        extraCss: "", // 打印可引入外部的一个 css 文件
        extraHead: "", // 打印头部文字
      },
      bhsShow:true,
      hsShow:false,
      pYf:"",
      sum:0,
      totalP:0,
      info:{},
      info2:{},
      cpList:[],
      // 遮罩层
      loading: true,
      dialogVisible: false,
      fullscreenLoading:false,
      // 选中数组
      ids: [],
      contractIds: [],
      // 非单个禁用
      single: true,
      textarea2:"",
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      radio:'1',

      // 合同管理表表格数据
      contractList: [],

      itembzinfoList:[],

      hbList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      wuOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gcCode: null,
        gcName: null,
        contractDate: null,
        hbType: null,
        hbCode: null,
        hbName: null,
        hbShuihao: null,
        hbAddress: null,
        hbPhone: null,
        hbBank: null,
        hbCard: null,
        hbName2: null,
        hbPhone2: null,
        contractId: null,
        contractQdDate: null,
        contractJhDate: null,
        contractQdName: null,
        item: null,
        itemName: null,
        spec: null,
        unit: null,
        contractUnitPrice: null,
        contractNum: null,
        jingbanren: null,
        tichengBili: null,
        tichengMoney: null,
        attach: null,
        remark: null,
        createDate: new Date()
      },

      queryParams2: {
        pageNum: 1,
        pageSize: 10000000,
      },
      // 表单参数
      form: {
        codeParams:[]
      },
      upload: {
        // 是否显示弹出层（物料导入）
        open: false,
        // 弹出层标题（物料导入）
        title: "导入合同信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/sales/contract/importData"
      },
      // 表单校验
      rules: {
        // hbName: [
        //   { required: true, message: "请选择合作伙伴", trigger: "blur" }
        // ],
      }
    };
  },
  watch:{
    $router(){
      this.getList();
    }
  },
  created() {
    this.getList();
    listFiles(this.queryParams2).then(response => {
      this.hbList = response.rows;
    });
  },
  filters: {
    numFilter (value) {
      // 截取当前数据到小数点后两位
      let realVal = parseFloat(value).toFixed(2)
      return realVal
    }
  },
  methods: {
    htInfo(row){
      let uuid = row.uuid
      getContract(uuid).then(res=>{
        this.wuOpen = true
        this.itembzinfoList=res.data.itemList
      })
    },
    tableRowClassName({row, rowIndex}) {
      if (row.isGenOrder === '0') {
        return 'warning-row';
      }
      return '';
    },
    hs(){
      // printBill
      // const style = '@page{margin:0};.title{fontSize:30px}'
      const style = '@page { margin:0 } ' +'@media print { #title{ font-size:40px};#title1{ font-size:40px }  ' ;
      printJS({
        printable:this.$refs.printBill,
        type:'html',
        header:'',
        targetStyles:['*'],
        scanStyles: false,
        style:style,
        onPrintDialogClose: () => {
          console.log('弹窗关闭');
        },
        onLoadingEnd:()=>{
          console.log('加载完毕')
        }
      })
    },
    bhs(){
      // printBill
      const style = '@page { margin:0 } ' +'@media print { #title{ font-size:40px};#title1{ font-size:40px }  ' ;
      printJS({
        printable:'printBillbhs',
        type:'html',
        header:'',
        targetStyles:['*'],
        scanStyles: false,
        style:style,
        onPrintDialogClose: () => {
          console.log('弹窗关闭');
        },
        onLoadingEnd:()=>{
          console.log('加载完毕')
        }
      })
    },
    //打印
    printt(){
      this.reset();
      const uuid = this.ids
      this.sum=0
      this.totalP=0
      // let that = this
      getContract(uuid).then(response => {
        this.ids=[]
        this.info = response.data
        // if(response.data.state!='1'){
        //   this.$modal.msgError("改合同未审批或审批拒绝，暂时不能被打印！");
        //   return;
        // }
        this.cpList = this.info.itemList
        console.log(this.cpList,'this.cpList')
        for(let i=0;i<this.cpList.length;i++){
          this.sum+=(this.cpList[i].orderNum-0)
          this.totalP+=(this.cpList[i].hsbj-0)*(this.cpList[i].orderNum-0)
        }
        let hbCode = response.data.hbCode
        let result = this.hbList.find(item => item.clientCode == hbCode)
        this.info2 = result
        // that.

        this.pYf = result.clientName
        let arr = this.cpList.find(item=> item.taxRate != 0)
        this.$nextTick(function () {

          if(arr=='' || arr==null || arr==undefined){
            this.bhs()
          }else{
            this.hs()
          }

        })
      });
    },
    approval(){
       this.dialogVisible = true;
    },

    applQd(){
      const uuid =  this.ids[0]
      let radio = this.radio;
      let param = {
        uuid,
        state:radio
      }
      applQd(param).then(res=>{
        this.getList();
        this.$modal.msgSuccess("审批完成！");
        this.dialogVisible = false;
        // this.$router.push('/sale/htgl/contract')
      });
    },
    //根据合同号获取物料
    // getWulist(){
    //   console.log(this.info,'--------->',this.info2)
    // },
    nameSelect(e){
      let result = this.hbList.find(item => item.clientCode ==e)
      this.form.hbType = result.clientType
      this.form.hbCode = result.clientCode
      this.form.hbShuihao = result.clientEin
      this.form.hbAddress = result.clientAdress
      this.form.hbPhone = result.clientPhone
      this.form.hbBank = result.clientBank
      this.form.hbCard = result.clientAcc
      this.form.hbName2 = result.clientLinker

      // this.hbList.filter(item=>{})
    },
    /** 查询合同管理表列表 */
    getList() {
      this.loading = true;
      this.queryParams.createDate = moment.utc(this.queryParams.createDate).local().format('YYYY-MM-DD')
      if(this.queryParams.createDate == 'Invalid date'){
        this.queryParams.createDate = null
      }
      console.log(this.queryParams.createDate,"his.queryParams.createDate")
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleOrder(){
      const loading = this.$loading({
        lock: true,
        text: '正在生成订单，请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      const uuid = this.ids[0]
      scOrder(uuid).then(response => {
        this.$modal.msgSuccess(response.msg);
        this.$router.push('/sale/xsdd/salesorder')
        loading.close();
      }).catch(reason=>{
        loading.close();
      });
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        gcCode: null,
        gcName: null,
        contractDate: null,
        hbType: null,
        hbCode: null,
        hbName: null,
        hbShuihao: null,
        hbAddress: null,
        hbPhone: null,
        hbBank: null,
        hbCard: null,
        hbName2: null,
        hbPhone2: null,
        contractId: null,
        contractQdDate: null,
        contractJhDate: null,
        contractQdName: null,
        item: null,
        itemName: null,
        spec: null,
        unit: null,
        contractUnitPrice: null,
        contractNum: null,
        jingbanren: null,
        tichengBili: null,
        tichengMoney: null,
        attach: null,
        bCode:null,
        remark: null
      };

      this.cpList = []
      this.info = []
      this.info2 = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uuid)
      this.contractIds = selection.map(item => item.contractId)
      this.single = selection.length!==1
      this.multiple = !selection.length

      let uuid = this.ids
      if(uuid.length == 1){
        getContract(uuid).then(response => {
          let cpList2 = response.data.itemList
          let arr = cpList2.find(item=> item.taxRate != 0)
          this.$nextTick(function () {
            if(arr=='' || arr==null || arr==undefined){
              this.bhsShow = true
              this.hsShow = false
            }else{
              this.hsShow = true
              this.bhsShow = false
            }
          })
        });
      }

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同管理表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uuid = row.uuid || this.ids
      getContract(uuid).then(response => {
        this.form = response.data;
        this.form.clientCode = response.data.hbCode;
        this.open = true;
        this.title = "修改合同管理表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uuid != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uuids = row.uuid || this.ids;
      this.$modal.confirm('是否确认删除合同管理表编号为"' + uuids + '"的数据项？').then(function() {
        return delContract(uuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
      const contractId = row.contractId || this.contractIds;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sales/contract/export', {
        ...this.queryParams
      }, `合同信息数据.xlsx`)
    },
    handleImport() {
      this.upload.title = "合同导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('sales/contract/importTemplate', {
      }, `合同数据信息.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    showAnnex(row){
      window.open(process.env.VUE_APP_BASE_API+row.attach)
    },
  }
};
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
